﻿package com.webbei.shoppingCar.action;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.apache.struts2.ServletActionContext;

import com.webbei.myCustom.action.MyUtil;
import com.webbei.pojo.Customer;
import com.webbei.pojo.Customs;
import com.webbei.pojo.PicInfor;
import com.webbei.pojo.shoppingCarResource;
import com.webbei.shoppingCar.service.shoppingCarService;

public class ShowShoppingCarAction {
	private shoppingCarService shopSer;
	private shoppingCarService scs;

	private Customer customer;
	//传入数据(Custom表的临时数据)
	private String customId;
	//传出参数：shoppingCarResource的LIST
	private List<shoppingCarResource> resList;
	//其他的传出参数
	private String receiveName;
	private String address;
	private String prov="浙江省";
	private String city="杭州市";
	private String area="江干区";
	private String shouji;
	private String zuoji1;
	private String zuoji2;
	private String remark;
	//支付宝数据
	private Integer listsize;
	private Double totalprice;
	private Long tid;
	private String tradename;
/*--------------------------------------------------------------------------------------*/
	public String execute() {
		System.out.println("进入直接付款页面");
		//初始化参数
		tid=new Long(0);
		resList = new ArrayList<shoppingCarResource>();
		Customs c = new Customs();
		shoppingCarResource s;
		Set<String> markSet=new HashSet<String>();
		String[] _customId = customId.split(",");
		for (String i : _customId) {
			c = shopSer.getCustomsById(Long.parseLong(i));
			s = new shoppingCarResource();
			s.setNumber(c.getCustomId());
			s.setTheme(c.getCustomName());
			s.setTime(c.getCustomTime());
			s.setPrice(c.getCustomPrice());
			s.setPic(c.getCustomPic());
			s.setOrderOrCustom(0);
			s.setContent(c.getCustomContent());
			s.setBgPic(c.getCustombgPic());
			s.setCount(1);
			List<PicInfor> l=MyUtil.deserialize(c.getCustomDetail(),c.getUserName(),0);
			String pi="";
			for(int ii=0;ii<l.size();ii++){
				PicInfor p=l.get(ii);
				pi+=p.getPicUrl()+"bz"+p.getHeight()+"bz"+p.getLeft()+"bz"+p.getTop()+"bz"+p.getWidth();
				if(ii!=l.size()-1){
					pi+="flag";
				}
			}
			s.setDetail(pi);
			resList.add(s);
			String username=(String) ServletActionContext.getContext().getSession().get("username");
			System.out.println(username);
			if(c.getUserName()!=null && c.getUserName().equals(username)){
				customer=scs.getCustomerByUserName(username);
				receiveName=c.getReceiveName()==null?customer.getUserRealName():c.getReceiveName();
				address=c.getReceiveAddress()==null?customer.getUserAddress():c.getReceiveAddress();
				prov=c.getReceiveProvince()==null?customer.getUserProvince():c.getReceiveProvince();
				city=c.getReceiveCity()==null?customer.getUserCity():c.getReceiveCity();
				area=c.getReceiveArea()==null?customer.getUserArea():c.getReceiveArea();
				shouji=c.getReceiveMobilephone()==null?customer.getUserMobilePhone():c.getReceiveMobilephone();
				String zuoji=c.getReceiveTelephone()==null?customer.getUsertelePhone():c.getReceiveTelephone();
				if(zuoji!=null&&!zuoji.equals("")&&!zuoji.equals("-")){
					zuoji1=zuoji.split("-")[0];
					zuoji2=zuoji.split("-")[1];
				}

				if(c.getCustomContent()!=null && !markSet.contains(c.getCustomContent())){
					markSet.add(c.getCustomContent());
				}
			}else if(c.getUserName()!=null && c.getUserName().equals("unLoginUserName")){
				receiveName=c.getReceiveName();
				address=c.getReceiveAddress();
				prov=c.getReceiveProvince();
				city=c.getReceiveCity();
				area=c.getReceiveArea();
				shouji=c.getReceiveMobilephone();
				String zuoji=c.getReceiveTelephone();
				if(zuoji!=null&&!zuoji.equals("")&&!zuoji.equals("-")){
					zuoji1=zuoji.split("-")[0];
					zuoji2=zuoji.split("-")[1];
				}
				if(c.getCustomContent()!=null && !markSet.contains(c.getCustomContent())){
					markSet.add(c.getCustomContent());
				}
			}
		}
		remark="";
		if(markSet.size()>0){
			for(String ss:markSet){
				remark+=(ss+",");
			}
			remark=remark.substring(0,remark.length()-1);
		}
		//支付宝数据
		listsize=new Integer(0);
		totalprice=new Double(0.0);
		tradename=new String("");
		System.out.println(resList);
		listsize=resList.size();
		if(tid==0){
			if(listsize==1){
				tid=resList.get(0).getNumber();
			}else{
				tid=scs.getNewTid();
			}
		}
		for(int cc=0;cc<resList.size();cc++){
			totalprice+=resList.get(cc).getPrice();
			if(cc!=0) tradename+=",";
			tradename+=resList.get(cc).getTheme();
		}
		if(prov==null) prov="浙江省";if(city==null) city="杭州市";if(area==null) area="江干区";
		return "success";
	}


	public shoppingCarService getShopSer() {
		return shopSer;
	}

	public void setShopSer(shoppingCarService shopSer) {
		this.shopSer = shopSer;
	}

	public Customer getCustomer() {
		return customer;
	}

	public void setCustomer(Customer customer) {
		this.customer = customer;
	}


	public List<shoppingCarResource> getResList() {
		return resList;
	}

	public void setResList(List<shoppingCarResource> resList) {
		this.resList = resList;
	}

	public String getCustomId() {
		return customId;
	}

	public void setCustomId(String customId) {
		this.customId = customId;
	}

	public shoppingCarService getScs() {
		return scs;
	}


	public void setScs(shoppingCarService scs) {
		this.scs = scs;
	}


	public Integer getListsize() {
		return listsize;
	}


	public void setListsize(Integer listsize) {
		this.listsize = listsize;
	}


	public Double getTotalprice() {
		return totalprice;
	}


	public void setTotalprice(Double totalprice) {
		this.totalprice = totalprice;
	}


	public Long getTid() {
		return tid;
	}


	public void setTid(Long tid) {
		this.tid = tid;
	}


	public String getTradename() {
		return tradename;
	}


	public void setTradename(String tradename) {
		this.tradename = tradename;
	}


	public String getReceiveName() {
		return receiveName;
	}


	public void setReceiveName(String receiveName) {
		this.receiveName = receiveName;
	}


	public String getAddress() {
		return address;
	}


	public void setAddress(String address) {
		this.address = address;
	}


	public String getProv() {
		return prov;
	}


	public void setProv(String prov) {
		this.prov = prov;
	}


	public String getCity() {
		return city;
	}


	public void setCity(String city) {
		this.city = city;
	}


	public String getArea() {
		return area;
	}


	public void setArea(String area) {
		this.area = area;
	}


	public String getShouji() {
		return shouji;
	}


	public void setShouji(String shouji) {
		this.shouji = shouji;
	}


	public String getZuoji1() {
		return zuoji1;
	}


	public void setZuoji1(String zuoji1) {
		this.zuoji1 = zuoji1;
	}


	public String getZuoji2() {
		return zuoji2;
	}


	public void setZuoji2(String zuoji2) {
		this.zuoji2 = zuoji2;
	}


	public String getRemark() {
		return remark;
	}


	public void setRemark(String remark) {
		this.remark = remark;
	}



}
